package com.fs.web.youzan.utils;

import com.youzan.cloud.open.sdk.api.API;
import com.youzan.cloud.open.sdk.core.client.auth.Token;
import com.youzan.cloud.open.sdk.core.client.core.DefaultYZClient;
import com.youzan.cloud.open.sdk.core.client.core.YouZanClient;
import com.youzan.cloud.open.sdk.core.oauth.model.OAuthToken;
import com.youzan.cloud.open.sdk.core.oauth.token.TokenParameter;
import com.youzan.cloud.open.sdk.gen.v3_0_0.api.YouzanItemsInventoryGet;
import com.youzan.cloud.open.sdk.gen.v3_0_0.api.YouzanItemsOnsaleGet;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanItemsInventoryGetParams;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanItemsInventoryGetResult;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanItemsOnsaleGetParams;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanItemsOnsaleGetResult;
import com.youzan.cloud.open.sdk.gen.v4_0_0.api.YouzanTradesSoldGet;
import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanTradesSoldGetParams;
import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanTradesSoldGetResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 有赞商城调用工具类
 */
@Slf4j
@Component
public class YouzanUtils {
    @Value("${youzan.clientId}")
    private String clientId;

    @Value("${youzan.clientSecret}")
    private String clientSecret;

    @Value("${youzan.grantId}")
    private String grantId;

    private String accessToken;

    /**
     * 获取有赞OAuthToken
     * @return 包含token等数据
     * @throws Exception
     */
    public OAuthToken getOAuthToken(){
        try {
            DefaultYZClient yzClient = new DefaultYZClient();
            TokenParameter tokenParameter = TokenParameter.self()
                    .clientId(clientId)
                    .clientSecret(clientSecret)
                    .grantId(grantId)
                    .build();
            OAuthToken oAuthToken = yzClient.getOAuthToken(tokenParameter);
            accessToken = oAuthToken.getAccessToken();
            return oAuthToken;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return new OAuthToken();
        }
    }

    /**
     * 获取有赞token
     */
    public String getToken(){
        if(StringUtils.isBlank(accessToken)){
            getOAuthToken();
        }
        return accessToken;
    }

    public <T> T getYouZanClient(API api, Token token, Class<T> aClass){
        try {
            YouZanClient yzClient = new DefaultYZClient();
            return yzClient.invoke(api, token, aClass);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    /**
     * 获取有赞出售中的商品
     * @return
     */
    public YouzanItemsOnsaleGetResult getSellShop(YouzanItemsOnsaleGetParams youzanItemsOnsaleGetParams){
        try {
            Token token = new Token(getToken());
            YouzanItemsOnsaleGet youzanItemsOnsaleGet = new YouzanItemsOnsaleGet();
            //设置参数
            youzanItemsOnsaleGet.setAPIParams(youzanItemsOnsaleGetParams);
            return getYouZanClient(youzanItemsOnsaleGet,token,YouzanItemsOnsaleGetResult.class);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    /**
     * 获取有赞仓库中的商品(未出售)
     * @return
     */
    public YouzanItemsInventoryGetResult getWarehouseShop(YouzanItemsInventoryGetParams youzanItemsInventoryGetParams){
        try {
            Token token = new Token(getToken());
            YouzanItemsInventoryGet youzanItemsInventoryGet = new YouzanItemsInventoryGet();
            //设置参数
            youzanItemsInventoryGet.setAPIParams(youzanItemsInventoryGetParams);
            return getYouZanClient(youzanItemsInventoryGet,token, YouzanItemsInventoryGetResult.class);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    /**
     * 获取有赞订单
     */
    public YouzanTradesSoldGetResult getOrder(YouzanTradesSoldGetParams youzanTradesSoldGetParams){
        try {
            Token token = new Token(getToken());
            YouzanTradesSoldGet youzanTradesSoldGet = new YouzanTradesSoldGet();
            //创建参数对象,并设置参数
            youzanTradesSoldGet.setAPIParams(youzanTradesSoldGetParams);
            return getYouZanClient(youzanTradesSoldGet, token, YouzanTradesSoldGetResult.class);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }
}
